package org.kite.processor;

import org.kite.MatchResult;

import java.text.MessageFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HeaderProcessor extends ProcessorBase {

	String regexPattern = "^(?<sharp>\\#{1,6})\\s+(?<content>.*)$";

	public MatchResult match(String line) {
		boolean isMatch = Pattern.matches(regexPattern, line);
		System.out.println(MessageFormat.format("[HeaderProcessor] matches [{0}] : {1}",
				line, isMatch));
		return isMatch ? MatchResult.Match : MatchResult.NotMatch;
	}

	public String process(List<String> lines) throws Exception {

		String line = lines.get(0);
		Pattern p = Pattern.compile(regexPattern);
		Matcher matcher = p.matcher(line);
		if (matcher.find()) {
			String sharps = matcher.group("sharp");
			String content = matcher.group("content");
			String htmlContent = MessageFormat.format("<h{0}>{1}</h{0}>",
					sharps.length(), content);
			System.out.println(MessageFormat.format("[HeaderProcessor] process [{0}] --> [{1}]",
					line, htmlContent));
			return htmlContent;
		}
		return line;
	}


}
